﻿using System;
using System.IO;
using System.Web;
using System.Web.SessionState;
using Wei8808Cms.Utility;
using Wei8808Cms.WebSite.UI;

namespace Wei8808Cms.WebSite.Service
{
    /// <summary>
    /// CommUpload 的摘要说明
    /// </summary>
    public class CommUpload : IHttpHandler, IRequiresSessionState
    {
        private readonly UpLoad _upFiles = new UpLoad();

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                var action = StrRequest.GetQueryString("action");
                if (string.IsNullOrEmpty(action)) return;
                var method = this.GetType().GetMethod(action);
                if (method == null) return;
                object[] parameters = { context };
                method.Invoke(this, parameters);
            }
            catch (Exception ex)
            {
                EventsLog.WriteLog("程序错误：" + ex.Message);
            }
        }
        /// <summary>
        /// 焦点图上传
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public void UploadAdbanner(HttpContext context)
        {
            HttpPostedFile upfile = context.Request.Files["FileUpload"];
            string delfile = context.Request.Params["txtImgUrl"];
            if (upfile == null)
            {
                context.Response.Write("{msg: 0, msbox: \"请选择要上传文件！\"}");
                return;
            }

            string msg = _upFiles.fileSaveAs(upfile, 0, "Adbanner");
            //删除已存在的旧文件
            if (!string.IsNullOrEmpty(delfile))
            {
                string filename = HttpContext.Current.Server.MapPath(delfile);
                if (File.Exists(filename))
                {
                    File.Delete(filename);
                }
            }
            //返回成功信息
            context.Response.Write(msg);
        }
        /// <summary>
        /// 上传产品功能小图标
        /// </summary>
        public void UploadProductIcon(HttpContext context)
        {
            var upfile = context.Request.Files["ProductIconFileUpload"];
            var delfile = context.Request.Params["txtProductIcon"];
            if (upfile == null)
            {
                context.Response.Write("{msg: 0, msbox: \"请选择要上传文件！\"}");
                return;
            }
            var msg = _upFiles.fileSaveAs(upfile, 1, "ProductIcon");
            //删除已存在的旧文件
            if (!string.IsNullOrEmpty(delfile))
            {
                var filename = HttpContext.Current.Server.MapPath(delfile);
                if (File.Exists(filename))
                {
                    File.Delete(filename);
                }
            }
            //返回成功信息
            context.Response.Write(msg);
        }
        /// <summary>
        /// 产品功能图片
        /// </summary>
        /// <param name="context"></param>
        public void UploadProductImg(HttpContext context)
        {
            var upfile = context.Request.Files["ProductImgFileUpload"];
            var delfile = context.Request.Params["txtProductImg"];
            if (upfile == null)
            {
                context.Response.Write("{msg: 0, msbox: \"请选择要上传文件！\"}");
                return;
            }
            var msg = _upFiles.fileSaveAs(upfile, 1, "ProductImg");
            //删除已存在的旧文件
            if (!string.IsNullOrEmpty(delfile))
            {
                var filename = HttpContext.Current.Server.MapPath(delfile);
                if (File.Exists(filename))
                {
                    File.Delete(filename);
                }
            }
            //返回成功信息
            context.Response.Write(msg);
        }

        /// <summary>
        /// 案例图片
        /// </summary>
        public void UploadCaseDummyImg(HttpContext context)
        {
            var upfile = context.Request.Files["CaseDummyImgFileUpload"];
            var delfile = context.Request.Params["txtCaseDummyImg"];
            if (upfile == null)
            {
                context.Response.Write("{msg: 0, msbox: \"请选择要上传文件！\"}");
                return;
            }
            var msg = _upFiles.fileSaveAs(upfile, 1, "CaseDummyImg");
            //删除已存在的旧文件
            if (!string.IsNullOrEmpty(delfile))
            {
                var filename = HttpContext.Current.Server.MapPath(delfile);
                if (File.Exists(filename))
                {
                    File.Delete(filename);
                }
            }
            //返回成功信息
            context.Response.Write(msg);
        }

        /// <summary>
        /// 案例图片
        /// </summary>
        public void UploadCaseImg(HttpContext context)
        {
            var upfile = context.Request.Files["CaseImgFileUpload"];
            var delfile = context.Request.Params["txtCaseImg"];
            if (upfile == null)
            {
                context.Response.Write("{msg: 0, msbox: \"请选择要上传文件！\"}");
                return;
            }
            var msg = _upFiles.fileSaveAs(upfile, 1, "CaseImg");
            //删除已存在的旧文件
            if (!string.IsNullOrEmpty(delfile))
            {
                var filename = HttpContext.Current.Server.MapPath(delfile);
                if (File.Exists(filename))
                {
                    File.Delete(filename);
                }
            }
            //返回成功信息
            context.Response.Write(msg);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}